package com.irdstudio.apicenter.console.controller.appmgr;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.irdstudio.apicenter.console.dm.AppBaseInfo;
import com.irdstudio.apicenter.console.dm.appinfo.AppBaseInfoService;
import com.irdstudio.apicenter.console.dm.util.FootPage;
import com.irdstudio.apicenter.console.dm.util.FootPageDao;
import com.irdstudio.apicenter.console.startup.MacmsRoot;
import com.irdstudio.apicenter.console.util.MacmsPathUtil;
import com.irdstudio.apicenter.core.dvo.RspValue;
import com.irdstudio.apicenter.frame.core.jdbc.tablemodel.ITableModelDAO;
import com.irdstudio.apicenter.frame.util.BeanUtils;

@Controller
@RequestMapping(value = "/appBaseInfo")
public class AppBaseAction {
	
	/* 基础工程名称 */
	private final static String baseProjectName = "BaseH5ProjectForPhone";
	
	@Autowired
	private AppBaseInfoService appBaseInfoService;

	/**
	 * 获取APP基本信息应用管理列表
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "queryList")
	public ModelAndView queryList(HttpServletRequest request) throws Exception {
		String sql = "";
		// 获取界面参数信息
		int row = 0;
		int size = 0;
		String id = "";
		String name = "";
		if (request.getParameter("pageNo") != null) {
			row = Integer.parseInt(request.getParameter("pageNo"));
		}
		if (request.getParameter("pageSize") != null) {
			size = Integer.parseInt(request.getParameter("pageSize"));
		}
		if (request.getParameter("useId") != null) {

			id = request.getParameter("useId");// 模块编码
		}
		if (request.getParameter("useName") != null) {

			name = request.getParameter("useName");// 模块编码
		}
		// 获取查询语句
		if (id.equals("") && !name.equals("")) {
			sql = "select app_id,app_name,app_type,app_version_id,app_version_date,app_state,app_project_state,app_project_date,app_project_url from APP_BASE_INFO where "
					+ "  app_name like '%" + name + "%' order by app_id";
		} else if (name.equals("") && !id.equals("")) {
			sql = "select app_id,app_name,app_type,app_version_id,app_version_date,app_state,app_project_state,app_project_date,app_project_url from APP_BASE_INFO where app_id like '%"
					+ id + "%' order by app_id";
		} else if (!name.equals("") && !id.equals("")) {
			sql = "select app_id,app_name,app_type,app_version_id,app_version_date,app_state,app_project_state,app_project_date,app_project_url from APP_BASE_INFO where app_id like '%"
					+ id + "%' and app_name like '%" + name + "%' order by app_id";

		} else {
			sql = "select app_id,app_name,app_type,app_version_id,app_version_date,app_state,app_project_state,app_project_date,app_project_url from APP_BASE_INFO order by app_id";
		}
		// 获取分页信息
		FootPageDao fpd = new FootPageDao();
		FootPage fp = new FootPage();
		// 获取sql的表名及条件
		String[] str = sql.split("from");
		fp = fpd.querypage(str[1].toString(), row, size);
		// 获取分页的起始编号
		row = (fp.getPageNo() - 1) * fp.getPageSize();
		ITableModelDAO tableModelDao = (ITableModelDAO) BeanUtils
				.getBean("tableModelDAO");
		sql = tableModelDao.getNativePageSql(sql, row, fp.getPageSize());
		// 传入参数获取列表信息，将结果推送到界面
		ModelAndView modelAndView = new ModelAndView();
		List<AppBaseInfo> appFrameModuleList = appBaseInfoService
				.queryList(sql);
		modelAndView.addObject("page", fp);
		modelAndView.addObject("useId", id);
		modelAndView.addObject("useName", name);
		modelAndView.addObject("AppModuleUseList", appFrameModuleList);
		modelAndView.setViewName("admin/appList");
		return modelAndView;
	}

	/**
	 * 新增app基本信息
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "addAppBaseInfo")
	public @ResponseBody
	ModelAndView addAppBaseInfo(
			@ModelAttribute("appBaseInfo") AppBaseInfo appBaseInfo)
			throws Exception {
		String appId = appBaseInfo.getAppId();
		String make = "0";
		// 创建对应APPID的文件夹
		String appPrjWs = MacmsPathUtil.getMacmsAppPrjPath(appId);
		if("".equals(appBaseInfo.getAppProjectUrl()) || appBaseInfo.getAppProjectUrl() == null){
			appBaseInfo.setAppProjectUrl(MacmsRoot.getSvnProjectService()
					.getServerUrl()+"/"+appBaseInfo.getAppId());
		}
		ModelAndView modelAndView = new ModelAndView();
		int bl = appBaseInfoService.addAppBaseInfo(appBaseInfo);
		System.out.println("appBaseInfo.getIsMake()===="+appBaseInfo.getIsMake()+"--projectUrl==="+appBaseInfo.getAppProjectUrl());
		//生成基础工程
		if(!"".equals(appBaseInfo.getIsMake()) && appBaseInfo.getIsMake() != null)
			make = appBaseInfo.getIsMake();
		if("0".equals(make)){
			// 导出基础工程到app工作空间
			System.out.println("正在导出基础工程...");
			if (!MacmsRoot.getSvnProjectService().exportProject(baseProjectName,
					appPrjWs)) {
				modelAndView.addObject("addflag", "导出工程失败!");
				modelAndView.addObject("ec", "0");
				return modelAndView;
			}
			// 将app工作空间下的工程提交到svn服务器
			System.out.println("正在提交基础工程...");
			RspValue rspValue = MacmsRoot.getSvnProjectService().addProject(appId);
			// 更新数据库信息
			/*if (rspValue.isOK()) {
				AppBaseInfoService appBaseInfoService = BeanUtils
						.getBean(AppBaseInfoService.class);
				if (!appBaseInfoService.updateAppSvnInfo(appId)) {
					//rspValue.resetToError();
					//rspValue.setEm("更新应用工程状态及地址信息出错!");
					modelAndView.addObject("addflag", "更新应用工程状态及地址信息出错!");
					modelAndView.addObject("ec", "0");
					return modelAndView;
				}
			}*/
		}else{
			System.out.println("正在导出工程...");
			if (!MacmsRoot.getSvnProjectService().exportProject(appId,
					appPrjWs)) {
				modelAndView.addObject("addflag", "导出工程失败!");
				modelAndView.addObject("ec", "0");
				return modelAndView;
			}
		}
		// 更新数据库信息
		System.out.println("更新数据库信息...");
		AppBaseInfoService appBaseInfoService = BeanUtils
				.getBean(AppBaseInfoService.class);
		if (!appBaseInfoService.updateAppSvnInfo(appId)) {
			modelAndView.addObject("addflag", "更新应用工程状态及地址信息出错!");
			modelAndView.addObject("ec", "0");
			return modelAndView;
		}
		if(bl == 0){
			modelAndView.addObject("addflag", "新增应用失败!");
			modelAndView.addObject("ec", "0");
		}else{
			modelAndView.addObject("addflag", "新增应用成功!");
			modelAndView.addObject("ec", "1");
		}
		return modelAndView;
	}

	/**
	 * 根据id获取当前id是否存在
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "findid")
	public @ResponseBody
	ModelAndView findid(HttpServletRequest request) throws Exception {
		// 传入参数获取列表信息，将结果推送到界面
		String id = request.getParameter("appid");
		ModelAndView modelAndView = new ModelAndView();
		int state = appBaseInfoService.findId(id);
		modelAndView.addObject("idflag", state);
		return modelAndView;
	}

	/**
	 * 创建应用基本信息
	 * 
	 * @param request
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "create")
	public String input(HttpServletRequest request) {
		request.setAttribute("svnUrl", MacmsRoot.getSvnProjectService()
				.getServerUrl());
		return "admin/appCreate";
	}

	/**
	 * 编辑应用基本信息
	 * 
	 * @param request
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "edit")
	public ModelAndView edit(HttpServletRequest request) throws Exception {

		String appId = request.getParameter("appid");
		if (StringUtils.isEmpty(appId)) {
			throw new Exception("必须传入appid参数");
		}

		AppBaseInfo appInfo = appBaseInfoService.findById(appId);
		ModelAndView modelAndView = new ModelAndView();
		if (appInfo != null) {
			modelAndView.addObject("AppBaseInfo", appInfo);
		}
		modelAndView.setViewName("admin/appBaseEdit");

		return modelAndView;
	}

	/**
	 * 更新app基本信息
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "updateAppInfo")
	public @ResponseBody
	ModelAndView updateAppInfo(
			@ModelAttribute("appBaseInfo") AppBaseInfo appBaseInfo)
			throws Exception {
		// 创建对应APPID的文件夹
		MacmsPathUtil.getMacmsAppResPath(appBaseInfo.getAppId());
		ModelAndView modelAndView = new ModelAndView();
		String bl = appBaseInfoService.updateAppInfo(appBaseInfo);
		modelAndView.addObject("editflag", bl);
		return modelAndView;
	}

}
